<!--TODO 没有个股推荐添加的界面-->
<% include ../header.ejs %>

<link rel="stylesheet" href="/stylesheets/app/recommend/recommed.css">

<header data-am-widget="header" data-am-sticky class="am-header am-header-default" id="appTemplates-Nav">
    <div class="am-header-left am-header-nav">
        <a href='javascript:history.go(-1)' class="">
            <img class="am-header-icon-custom" src="data:image/svg+xml;charset&#x3D;utf-8,&lt;svg xmlns&#x3D;&quot;http://www.w3.org/2000/svg&quot; viewBox&#x3D;&quot;0 0 12 20&quot;&gt;&lt;path d&#x3D;&quot;M10,0l2,2l-8,8l8,8l-2,2L0,10L10,0z&quot; fill&#x3D;&quot;%23fff&quot;/&gt;&lt;/svg&gt;" alt=""/>
        </a>
    </div>

    <h1 class="am-header-title">
        <a href="#title-link" class="" id="Typeface-whitecolor">新增个股推荐</a>
    </h1>

    <div class="am-header-right am-header-nav">
        <a id="a_save" onclick="javascript:;" class="" data-am-modal="{target: '#modal-type-add', closeViaDimmer: 0, width: 240, height: 260}">保存</a>
    </div>
</header>

<form name="addstockrecommend" class="am-form" method="post" action="save" data-am-validator>
    <fieldset>
        <div class="am-form-group">
            <label for="doc-vld-name">调入日期：</label>
            <div id="start_datetime" class="am-input-group am-datepicker-date" data-init="<%= mConference && mConference.updateTime%>" data-am-datepicker="{format: 'yyyy-mm-dd', viewMode: 'month'}">
                <input type="text" class="am-form-field" placeholder="选择日期" readonly id="updateTime" name="updateTime" required>
                <span class="am-input-group-btn am-datepicker-add-on">
                    <button class="am-btn am-btn-default" type="button"><span class="am-icon-calendar"></span> </button>
                </span>
            </div>
        </div>
        <div class="am-form-group">
            <label for="doc-vld-name">推荐来源:<%= (mConference && mConference.addType ==0) || addType == '0'?'内部推荐':'外部推荐'%></label>
            <input type="hidden" value="<%= (mConference && mConference.addType) || addType %>" name = "addType">
        </div>
        <div class="am-form-group">
            <label for="doc-vld-name">推荐公司:</label>
            <select class="company-data-ajax" style="width: 100%" placeholder="请选择上市公司" id="stockId" name="stockId" required>
                <%if(mConference && mConference.stockId){%>
                   <option value="<%=mConference.stockId.rowId%>" selected><%=mConference.stockId.stockCode +" "+ mConference.stockId.name%></option>
                <%}%>
            </select>
        </div>
        <div class="am-form-group" style="margin-top:5px;">
            <label for="doc-vld-name">推荐人公司:</label>
            <select name="seller" data-am-selected="{btnWidth:'100%'}" placeholder="选择券商" required id="seller">
                   <% seller.obj.forEach(function (data, index) {%>
                      <option value="<%=data.rowId%>" <%if(mConference&&mConference.seller?mConference.seller.rowId==data.rowId:index==0){%>selected<%}%>><%=data.name%></option>
                   <% })%>
            </select>
        </div>
        <div class="am-form-group"  style="margin-top: 5px;">
            <label for="doc-vld-name">推荐人:</label>
            <select data-am-selected="{btnWidth: '100%'}" placeholder="请选择推荐人" id="createUserId" name="createUserId" required>
                <% person.obj.forEach(function (data, index) { %>
                <option value="<%=data.rowId%>" <%if(mConference&&mConference.createUserId?mConference.createUserId.rowId==data.rowId:data.rowId==userinfo.rowId){%>selected<%}%>><%=data.name%></option>
                <% })%>
            </select>
        </div>
        <div class="am-form-group">
            <label for="doc-vld-name">调入价：</label>
            <input type="number" <% if(mConference && mConference.importPrice){ %>value="<%=mConference.importPrice%>"<% } %> id="importPrice" name="importPrice" placeholder="调入价" required>
        </div>

        <div class="am-form-group">
            <label for="doc-vld-name">目标价：</label>
            <input type="number" <% if(mConference && mConference.expectPrice){ %>value="<%=mConference.expectPrice%>"<% } %> id="expectPrice" name="expectPrice" placeholder="目标价" required>
        </div>


        <div class="am-form-group">
            <label for="doc-vld-name">观点：</label>
            <textarea class="" rows="5" id="reason" name="reason" placeholder="观点"><%= (mConference && mConference.reason)||'' %></textarea>
        </div>

        <button class="am-btn am-btn-secondary" type="submit" id="save" style="visibility:hidden">提交</button>


        <input class="" id="expectRate" name="expectRate" placeholder="目标涨幅" type="hidden">

        <%if(mConference && mConference.rowId){%>
        <input name="rowId" value="<%=mConference.rowId%>" type="hidden">
        <%}%>

    </fieldset>
</form>

<script type="text/javascript">
    $(function() {
        //设置默认时间为当天
        $('#start_datetime').datepicker('setValue', $('#start_datetime').data('init') || new Date().format('yyyy-MM-dd'));

        //自动填入当天价格
        $('#stockId').on('change', function() {
            var stockNoName=  $('#stockId').find("option:selected").text();
            stockNo=stockNoName.split(" ")[0];
            $.get("/smtool_project/wxapp/api/stock/getRealPrice?stockCode="+stockNo,function(repdata,status){
                $('#importPrice').val(repdata);
                $('#importPrice').focus();
            });
        });

         //修改person值
         $('#seller').on('change', function() {
             var seller=  $('#seller').val();
             $.get("/smtool_project/wxapp/api/user/getUserListBySeller?seller="+seller,function(repdata,status){
                    $('#createUserId').children().remove();
                    $.each(repdata,function(i,obj){
                        $('#createUserId').append("<option value='"+obj.rowId+"' >"+obj.name+"</option>");
                    });
             });
         });

    });
    $(".company-data-ajax").select2({
        ajax: {
            url: "../../api/dicStock/getStockList",
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    name: params.term
                };
            },
            processResults: function (data, params) {
                params.page = params.page || 1;
                var newdata = $.map(data.obj, function (obj) {
                    obj.id = obj.id || obj.rowId; // replace pk with your identifier
                    obj.text = obj.text ||obj.stockCode+" "+ obj.name;
                    return obj;
                });

                return {
                    results: newdata,
                    pagination: {
                        more: (params.page * 30) < data.total_count
                    }
                };
            },
            cache: true
        },
        escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
        minimumInputLength: 2
    });

    //保存功能
    $(function() {
        $('#a_save').on('click', function() {
            var importPrice=  $('#importPrice').val();
            var expectPrice= $('#expectPrice').val();
            var expectRate=  getExpectRate(importPrice,expectPrice)

            $('#expectRate').val(toDecimal(expectRate)+"%")
            $('#save').click()
        });
    });


    function getExpectRate(importPrice,expectPrice)
    {
        return (expectPrice-importPrice)*100/importPrice
    }

    function toDecimal(x) {
        var f = parseFloat(x);
        if (isNaN(f)) {
            return;
        }
        f = Math.round(x*100)/100;
        return f;
    }
</script>
<% include ../footer.ejs %>